package com.shxc.smarttrade.old.presenter;

import android.os.Handler;

import com.shxc.smarttrade.old.bean.OpenTrader;
import com.shxc.smarttrade.old.bean.PendingOrder;
import com.shxc.smarttrade.old.bean.SymbolInfo;
import com.shxc.smarttrade.old.bean.SymbolPrice;
import com.shxc.smarttrade.old.iview.IViewStockChart;
import com.shxc.smarttrade.old.modal.IRequestServiceListener;
import com.shxc.smarttrade.old.modal.StockChartModal;

/**
 * Created by jcdev1 on 2017/5/24.
 */

public class StockChartPresenter {
    private StockChartModal modal;
    private IViewStockChart iViewStockChart;
    private Handler handler = new Handler();

    public StockChartPresenter(IViewStockChart iViewStockChart) {
        this.modal = new StockChartModal();
        this.iViewStockChart = iViewStockChart;
    }

    /**
     * 品种保证金及止盈止损数据接口
     *
     * @param symbol
     */
    public void getSymboLCalInfo(String accessToken, String symbol, int userId) {
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                iViewStockChart.showDialog();
//            }
//        });
        modal.getSymboLCalInfo(accessToken, symbol, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.setSymbolCalInfoData((SymbolInfo) result);
//                        iViewStockChart.dismissDialog();
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.showMyToast(msg);
//                        iViewStockChart.dismissDialog();

                    }
                });
            }
        });
    }

    /**
     * open
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     */
    public void wareHouse(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                iViewStockChart.showDialog();
            }
        });
        modal.wareHouse(accessToken, symbol, userId, cmd, volume, tp, sl, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.dismissDialog();
                        iViewStockChart.setTradeOpenData((OpenTrader) result);
//                        iViewStockChart.showMyToast("开仓成功");
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.dismissDialog();
                        iViewStockChart.showMyToast(msg);
                    }
                });
            }
        });
    }

    /**
     * 挂单
     *
     * @param symbol
     * @param userId
     * @param cmd
     * @param volume
     * @param tp
     * @param sl
     * @param pendingPrice
     * @param market
     */
    public void pengingOrder(String accessToken, String symbol, int userId, int cmd, float volume, double tp, double sl, double pendingPrice, double market) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                iViewStockChart.showDialog();
            }
        });
        modal.pengingOrder(accessToken, symbol, userId, cmd, volume, tp, sl, pendingPrice, market, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.dismissDialog();
                        iViewStockChart.setPendingOrderData((PendingOrder) result);
//                        iViewStockChart.showMyToast("挂单成功");
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.dismissDialog();
                        iViewStockChart.showMyToast(msg);
                    }
                });
            }
        });
    }

    public void getSymbolPrice(String accessToken, String symbol) {
        modal.getBaseData(accessToken, symbol, new IRequestServiceListener() {
            @Override
            public void success(Object result) {
                iViewStockChart.setSymbolPrice((SymbolPrice) result);
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewStockChart.showMyToast(msg);
                    }
                });
            }
        });
    }
}
